$.eud.mdm = $.eud.mdm || {};

$.eud.mdm.mdmStoreGroup = {
	//基类名字
	pageName:"BaseSimplePage",
	init:function(){
		var me = this;
		$.extend($.fn.BaseSimplePage._config.grid,{onGridBeforeCellEdit:function(changes,index,field,val){
//            var changes = $(grid1).datagrid('getRows')[rowindex]; 
            if(field == 'storeGroupNo'){
            	var grid1 = this;
            	var ed = $(grid1).datagrid('getEditor', {index:0,field:'isLeaf'});
            	var tag = "1";
            	if(ed){
            		tag = $(ed.target).datebox('getValue');
            	}else{
            		tag = changes.isLeaf;
            	}
            if(tag == "0"){
            	 $(grid1).datagrid("getColumnOption","storeGroupNo").editor=
                     {
     	        		type:"textbox",
     	        		options:{required:true}
     	        	}
            }else if(tag == "1"){
            	 $(grid1).datagrid("getColumnOption","storeGroupNo").editor={
	        		type:"searchbox",
	        		"options": {
							"required": true,
							"showWin": true,
							"winConfing" :{
					    		"type":"mdmStoreSearchWin",
					    		"canAddRows":true,
					    		isFilterAll:true,
					    		 returnValues:[{"field":"storeGroupNo","map":"storeNo"},{"field":"storeGroupName","map":"storeName"}],
					    		"formParams":[
					    		  {field:"storeGroupNo",map:"storeNo"}
					    		]
				    		}
						}
	        	}
            }
            }
            }});
        var page = $("#mdmStoreGroup")[me.pageName]();
	},
	/**
     * 初始化页面事件
     */
    initEvents: function(){
    	var me =this;
    	// 按钮事件绑定
    	$("#toolbar_refresh").bind("click", function(e){
    		me.initTree();
    	});
    	
    	// 初始化页面
    	me.initPage();
    },
    data:{
        statusData:[{"itemCode":"1","itemName":"启用"},{"itemCode":"0","itemName":"禁用"}]//启用状态
    },
    formatter:{
        status:function(val,item){
            return $.eud.utils.comboboxFormatter(val, "itemCode","itemName",  $.eud.mdm.mdmStoreGroup.data.statusData);
        }
    },
    //页面初始化
    initPage: function(){
    	var me = this;
    	// 初始化树
    	me.initTree();
    },
  //下拉框数据源
    isLeafFlagData:[{"id":"1","text":"是"},{"id":"0","text":"否"}],//启用状态
    formatter:{
    	isLeafFlagData:function(val,item){
			return $.eud.utils.comboboxFormatter(val, "id","text",  $.eud.mdm.mdmStoreGroup.isLeafFlagData);
		}
	},
	
	 //初始化tree
    initTree: function(){
    	var me = this;
    	var  t=$('#tt').tree({
    		data:[],
    		method: 'post',
    		animate:true,	//动画效果
    		onBeforeExpand : function(node){
    			me.onTreelClick(node);
    		}
    	});
        $.eud.msg.maskMager.mask("加载中...");
        $.eud.loader.ajax(BasePath + "/mdm_store_group/findGrop.json",{},function(data){
            $.eud.msg.maskMager.unmask();
            t.tree("loadData",data.data);
        },function(){
            $.eud.msg.maskMager.unmask();
        });
    	
    },
    
    //树节点击事件
    onTreelClick: function(node){
    	if(node.isLoad) return;//判断是否已经加载过数据
    	//return;
        $.eud.msg.maskMager.mask("加载中...");
        $.eud.loader.ajax(BasePath + "/mdm_store_group/findTreeById.json?storeGroupNo="+node.storeGroupNo+"&isLeaf="+node.isLeaf,{},function(data){
            $.eud.msg.maskMager.unmask();
            node.isLoad = true;
            $('#tt').tree('append',{
    			parent:node.target,
    			data:data.data
    		});
        },function(){
            $.eud.msg.maskMager.unmask();
        },false);
    	//加载网格
    	$('#masterGrid').datagrid('load',{
    		storeGroupNo :node.storeGroupNo
    	});
    	
    },
    // 刷新（树 && 网格）
    refresh: function(){
    	$('#tt').tree('reload');
    	//$('#masterGrid').datagrid('reload');
    	
    	var rootNode = $('#tt').tree('getRoot');
    	$('#tt').tree('expand', rootNode.target);
    }, 
    
    //重载方法
    override:{
    	onBtnBeforeAdd:function(e, scope,row){
    		var nodes = $('#tt').tree('getSelected');
    		if(nodes ==null ||nodes.isLeaf ==1){
    			$.eud.msg.warning("请选择节点或已经是叶子节点不能新增！！");
    			return false;
    		}
    		row['pStoreGroupNo']=nodes.storeGroupNo;
    	},
    	/*onBeforeDelete:function(e, scope,row){
    		var nodes = $('#tt').tree('getSelected');
    		
    		$.eud.msg.warning("不能删除！！");
    		return false;
    	}*/
    }
	
};